<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_upload_file
    //
	class t_field_upload_file extends t_base_field
	{
        
        // property declaration
		// protected
		// B
		protected $button_tag = null;
		protected $form_tag = null;
		protected $iframe_tag = null;
        

        // property declaration
		// public

		
		// G
		const language_position_upload_button = 1;
		const language_position_uploaded_ok_message = 2;
		const language_position_uploaded_err_message = 3;
		const language_position_last = 4;
		
		// P
		const proper_properties_position_per_destination_folder = 1;
		const proper_properties_position_per_use_custom_file_result = 2;
		const proper_properties_position_per_use_custom_language = 3;
		const proper_properties_position_per_accept = 4;
        const proper_properties_position_last = 5;

        // method declaration
    	// protected
		
		// C
        // G
		protected function get_action_name($a_prefix)
        {
            return $a_prefix . "_" . $this->name;
        } 
		
        
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			//echo "a_event_type:$a_event_type<BR>";

			$local_return = '';
			switch ($a_event_type) 
            {
				case t_base_field::field_event_type_on_selected:
						//$this->default_start_tag->add_property("onclick","timeInterval=setInterval('".t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)',1500);");
				break;       
				
			}
			return $local_return;
		}
		protected function get_method_name()
        {	
			return "start_upload";
			
		}
		static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			switch ($a_index) 
            {
				case self::proper_properties_position_per_destination_folder:
					$local_return = '';
				break; 
				case self::proper_properties_position_per_use_custom_file_result:
					$local_return = 'FALSE';
				break; 
				case self::proper_properties_position_per_use_custom_language:
					$local_return = 'FALSE';
				break; 
				case self::proper_properties_position_per_accept:
					$local_return = 'image/*';
				break; 	
                             
			}
			return $local_return;
		}
		protected function get_proper_properties_position_last()      
		{
            return self::proper_properties_position_last;
        }
		
	
		
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str)
		{
            global $g_ambient_namespace;
			parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            $this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_field_method","'.$this->get_action_name('set_event').'",["'.$this->name.'"],"'.t_base_field::field_event_type_on_selected.'","'.$this->get_event_name().'","'.$this->get_method_name().'"]');
			//echo "t_action_execute_field_method: ".$this->get_action_name('set_event')."<br>";
			if (strtoupper($this->proper_properties_array[self::proper_properties_position_per_use_custom_language]) == 'TRUE')
                $g_ambient_namespace->load_custom_language($this,$this->name);
		}
		// D

		public function initialize_main_tag()
        {	
           // $this->main_tag = parent::get_parent_tag(); 
			/*$local_name = $this->common_properties_array[t_base_field::common_properties_position_per_field_name];
			echo "initialize_main_tag:$initialize_main_tag<br>";
			$local_current_tag = parent::add_tag(t_base_tag_type::btt_div,
                $local_name);
			$this->main_tag = $local_current_tag;*/
			$this->main_tag = parent::get_parent_tag(); 
			$this->default_start_tag = $this->main_tag;
		}
        // C
		public function create_tags_structure()
        {
            //echo __METHOD__."<BR>";
            global $g_javascript_namespace;
			global $g_cuteengine;
			parent::create_tags_structure();
			// method

			// component
			$local_tag_form = parent::add_tag(t_base_tag_type::btt_form,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_form");
				
			
			$local_tag_form->add_property('method','post');
			
			$local_parameters_list = new t_url_parameters_list(false);
			$local_parameter = $local_parameters_list->add_new_parameter(
                $g_cuteengine->id,'do_action',
                "@".md5($this->get_action_name('set_event')),
                t_url_parameter::request_type_no_page);	
            //
			//echo "t_action_execute_field_method: ".$this->get_action_name('set_event')."<br>";
			$local_url = $local_parameters_list->compute_http_predefined_variables();
			
			$local_tag_form->add_property('action',$local_url);
			$local_tag_form->add_property('target',$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_iframe");
			$local_tag_form->add_property('enctype','multipart/form-data');
			$this->form_tag = $local_tag_form;
			$local_current_tag = $local_tag_form->create_and_add_child_at_bottom(t_base_tag_type::btt_file,
                $this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_input");
			
			if (trim($this->proper_properties_array[self::proper_properties_position_per_accept]) != '')
				$local_current_tag->add_property('accept',$this->proper_properties_array[self::proper_properties_position_per_accept]);
            $this->button_tag = $local_tag_form->create_and_add_child_at_bottom(t_base_tag_type::btt_button,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_button");
			$this->button_tag->set_text_to_include(
				$this->lang[self::language_position_upload_button]);
				
			$this->default_start_tag = $this->button_tag;
            $this->default_result_tag = $this->main_tag;
			$local_iframe_tag = $local_tag_form->create_and_add_child_at_bottom(t_base_tag_type::btt_iframe,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_iframe");
			$this->iframe_tag = $local_iframe_tag;
            // label style
            $this->add_style_sheet_property("#".$this->main_tag->name."","width","700px");
            $this->add_style_sheet_property("#".$this->main_tag->name." label","display","block");
            $this->add_style_sheet_property("#".$this->main_tag->name." label","width","100%");
            // input, textarea style
            $this->add_style_sheet_property("#".$this->main_tag->name." input, textarea","display","block");
            $this->add_style_sheet_property("#".$this->main_tag->name." input, textarea","width","70%");
            $this->add_style_sheet_property("#".$this->main_tag->name." input, textarea","margin-right","10px");
            $this->add_style_sheet_property("#".$this->main_tag->name." input, textarea","margin-left","10px");
            $this->add_style_sheet_property("#".$this->main_tag->name." input, textarea","margin-bottom","10px");

            // button style
            $this->add_style_sheet_property("#".$this->main_tag->name." button","display","block");
            $this->add_style_sheet_property("#".$this->main_tag->name." button","padding","3px");
            // event
			$this->get_field_event_script(t_base_field::field_event_type_on_selected,"event");
			
            
        }
		public function create_javascript_initialization_component()
		{
			global $g_javascript_namespace;
            global $g_strings_utility_namespace;
			// Methods    
			// P
			/*$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
				get_class($this),'prepare_childs_values_to_send','',"
	var local_result = '';
	var local_parent = ".t_cuteengine::javascript_instance_name.".html_tag(this.name);
	var e = 0;
	for (var i = 0; i < local_parent.childNodes.length; i++) 
	{
		var child = local_parent.childNodes[i];
		if ((child.nodeName == 'INPUT') || (child.nodeName == 'TEXTAREA'))
		{
			var local_ce = ".t_cuteengine::javascript_instance_name."[child.id];
            if ((local_ce != undefined) && (local_ce.check_value != undefined) &&  (!local_ce.check_value()))
                return false;
            ".$g_strings_utility_namespace->get_javascript_code_to_hide_separetor("e","local_result","child.value")."
			e++;
		}
	} 
	return local_result;
	");*/
		}
        
		// G
		public function get_event_name()
        {

            return /*get_class($this)*/$this->name . "_event_".$this->get_event_type('');
        }		
        public function get_event_type($a_action_name)
        {	
			return t_base_field::field_event_type_on_selected;
			
		}		
// G
		public function get_included_in_html_structure($a_name,$a_display,$a_key_tag_name)
		{
			//parent::get_included_in_html_structure($a_name);
			/*if ($a_display)
			{
				if (isset($this->label_tag))
					$this->label_tag->add_style_property('display','');
				$this->main_tag->add_style_property('display','');
				$this->message_tag->add_style_property('display','');
			}
			else
			{
				if (isset($this->label_tag))
					$this->label_tag->add_style_property('display','none');			
				$this->main_tag->add_style_property('display','none');
				$this->message_tag->add_style_property('display','none');
			}	
			$this->main_tag->remove_property('disabled');*/
			
			$this->key_tags_list[$a_name] = $a_key_tag_name;
			//echo "a_name: $a_name - a_key_tag_name: $a_key_tag_name<br>";
			$local_label_script = '';
			return '';//($this->form_tag->generate_html_code_with_new_name($a_name));
		}
		
		public function get_included_in_html_structure_no_edit_mode($a_id,$a_value,$a_label_value)
		{
			global $g_cuteengine;
			/*if (isset($this->label_tag))
				$this->label_tag->add_style_property('display','');			
			$this->main_tag->add_style_property('display','');
			$this->message_tag->add_style_property('display','');
			//echo "a_value: $a_value<br>";
			if ($a_value == 1) 
			{
				
				$this->main_tag->add_property('checked','checked');
			}
			else	
				$this->main_tag->remove_property('checked');
			$this->main_tag->add_property('disabled','disabled');	
			$local_label_script = '';
			if (isset($this->label_tag))
				$local_label_script = $this->label_tag->generate_html_code_with_new_name($a_id.t_field_text::postfix_label_tag_name);*/
			$local_parameters_list = new t_url_parameters_list(false);
			$local_parameter = $local_parameters_list->add_new_parameter(
                $g_cuteengine->id,'do_action',
                t_url_parameter::separator_level_0.md5($this->get_action_name('set_event')).t_url_parameter::separator_level_0.t_url_parameter::separator_level_0.$a_value,
                t_url_parameter::request_type_no_page);	
			
			$local_url = $local_parameters_list->compute_http_predefined_variables();
			// form_tag
			$this->form_tag->add_property('action',$local_url);
			$this->form_tag->add_property('target',$a_id."_iframe");
			// iframe tag
			$this->iframe_tag->name = $a_id."_iframe";
			// result 
			
			return ($this->form_tag->generate_html_code_with_new_name($a_id));
			
			
		}		
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
		public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
            return '';
        }
		// I
		public function initialize_field_event($a_event_type)
        {	
			global $g_javascript_namespace;
            global $g_strings_utility_namespace;
            global $g_url_parameters_namespace;
            $local_parameter = $g_url_parameters_namespace->get_parameter_by_component_id($this->parent_component->id);
			$local_event = $this->events[$a_event_type];
			//echo "local_event->event_name: ".$local_event->event_name." - local_event->event_type: $local_event->event_type<br>";
			switch ($local_event->event_type) 
            {
                /***********  t_base_field::field_event_type_on_selected  ***********/                
				case t_base_field::field_event_type_on_selected:
					$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
						get_class($this),$local_event->event_name,'event',"
	   var local_argoument = '';
       ".$local_event->body_code."
       ");
				break;

			}	
		}
		// S
		public function start_upload($a_parameter,$a_result)
		{
			//echo "start_upload CALLED<br>";
			//print_r($_FILES);
			
			//print_r($a_parameter);
			$local_file_name = trim(str_replace('.php','',$this->proper_properties_array[self::proper_properties_position_per_use_custom_file_result]));
			//echo "local_file_name: $local_file_name<br>";
			if ((strtoupper($local_file_name) !== 'FALSE') && (trim($local_file_name) !== ''))
			{
				$local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
				require($local_file_name);
			}
			else if(isset($_FILES[$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_input"]))
			{
			
				$local_file_uploaded = $_FILES[$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_input"];
				//echo "local_file_uploaded['name']: ".$local_file_uploaded['name']." - ERROR: ".$local_file_uploaded['error']."<br>";
				//$ext = pathinfo($local_file_uploaded, PATHINFO_EXTENSION);
				/*$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
				$detectedType = exif_imagetype($local_file_uploaded['tmp_name']);
				$error = !in_array($detectedType, $allowedTypes);*/
				if(($local_file_uploaded['error'] == 0) && (is_uploaded_file($local_file_uploaded['tmp_name'])))
				{
					$local_destination = $this->proper_properties_array[self::proper_properties_position_per_destination_folder];
					if (trim($local_destination) == '')
						$local_destination = CUTEENGINE_PATH_CUSTOM_FILES;
					move_uploaded_file($local_file_uploaded['tmp_name'], $local_destination.$local_file_uploaded['name']);
					//echo "MOVED TO ".$local_destination.$local_file_uploaded['name']."<br>";
					$a_result->succeeded = true; 
					$a_parameter->append_to_argument_value('3_1',$local_destination.$local_file_uploaded['name']);
					echo $this->lang[self::language_position_uploaded_ok_message];
				}
				else
				{
					$a_result->succeeded = false;
					echo $this->lang[self::language_position_uploaded_err_message];
				}
			}
			
		}
		
    }
?>    